package com.example.controller;

import com.example.pojo.ResponseMessage;
import com.example.pojo.User;
import com.example.pojo.dto.UserDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.example.service.IUserService;

@RestController // 接口方法返回对象 转换成json
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService useService;

    // @Validated 验证参数的注解
    // 增加
    @PostMapping("/add")
    public ResponseMessage<User> addUser(@Validated @RequestBody UserDto user) {
        System.out.println("~~~~~~~~~~~~~~~");
        System.out.println(user.toString());
        User userNew = useService.add(user);
        System.out.println(userNew.toString());
        return ResponseMessage.success(userNew);
    }

    // 查询
    @GetMapping("/{userId}")
    public ResponseMessage<User> getUser(@PathVariable Integer userId) {
        System.out.println("查询");
        User userInfo = useService.getUser(userId);
        return ResponseMessage.success(userInfo);
    }

    // 修改
    @PutMapping
    public ResponseMessage<User> edit(@Validated @RequestBody UserDto user) {
        User userNew = useService.edit(user);
        return ResponseMessage.success(userNew);
    }

    // 删除
    @DeleteMapping("/{userId}")
    public ResponseMessage<String> delete(@PathVariable @RequestBody Integer userId) {
        useService.delete(userId);
        return ResponseMessage.success("删除成功");
    }
}
